package com.syh.jn.scm.service.impl.downstream.base;

import cn.redscarf.commons.util.other.ExtAssert;
import cn.redscarf.commons.util.validator.ValidationUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.syh.core.ddd.infrastructure.repository.AggregateRepository;
import com.syh.core.ddd.service.AbstractServiceImpl;
import com.syh.jn.scm.domain.downstream.entity.base.DownBusinessEntityInvoiceInfo;
import com.syh.jn.scm.domain.downstream.repository.base.DownInvoiceInfoRepository;
import com.syh.jn.scm.dto.downstream.base.DownInvoiceInfoMto;
import com.syh.jn.scm.dto.downstream.base.DownInvoiceInfoSto;
import com.syh.jn.scm.dto.downstream.base.DownInvoiceInfoVo;
import com.syh.jn.scm.service.api.downstream.base.DownBusinessEntityInvoiceInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class DownBusinessEntityInvoiceInfoServiceImpl extends AbstractServiceImpl<DownInvoiceInfoSto, DownInvoiceInfoMto, DownInvoiceInfoVo, DownBusinessEntityInvoiceInfo, Long> implements DownBusinessEntityInvoiceInfoService {
  @Autowired
  private DownInvoiceInfoRepository downBusinessEntityInvoiceInfoRepositoryImpl;

  @Override
  protected AggregateRepository<DownBusinessEntityInvoiceInfo, Long> getAggregateRepository() {
    return downBusinessEntityInvoiceInfoRepositoryImpl;
  }

  /**
   * 保存对象
   * @param sto
   * @return
   */
  @Override
  public Long save(DownInvoiceInfoSto sto) {
    ExtAssert.notNull(sto, "对象为空");
    ValidationUtils.validate(sto);

    DownBusinessEntityInvoiceInfo basicInfo = DownBusinessEntityInvoiceInfo.createTicketBasicInfo(sto);
    return downBusinessEntityInvoiceInfoRepositoryImpl.add(basicInfo);
  }

  /**
   * 修改对象
   * @param mto
   */
  @Override
  public void update(DownInvoiceInfoMto mto) {
    ExtAssert.notNull(mto, "对象为空");
    ValidationUtils.validate(mto);

    DownBusinessEntityInvoiceInfo basicInfo = downBusinessEntityInvoiceInfoRepositoryImpl.getById(mto.getId());
    ExtAssert.notNull(basicInfo, "对象为空");

    basicInfo.modify(mto);
    downBusinessEntityInvoiceInfoRepositoryImpl.update(basicInfo);
  }

  /**
   * 删除对象
   * @param id
   */
  @Override
  public void delete(Long id) {
    ExtAssert.notNull(id, "id为空");

    DownBusinessEntityInvoiceInfo basicInfo = downBusinessEntityInvoiceInfoRepositoryImpl.getById(id);
    ExtAssert.notNull(basicInfo, "对象为空");

    basicInfo.destroy();
    downBusinessEntityInvoiceInfoRepositoryImpl.remove(basicInfo);
  }
}
